<?
 class view_radio{
	private $name;
	private $title;
	private $crucial_get_param;
	private $default_key;
    private $current_key;
	private $other_selected = array();
	private $hotkeys = array();
	private $action_params = array();
	private $items = array();
	private $get_params;
	private $urls;
	public function __construct($name, $crucial_get_param = "Act"){
		$this->name = $name;
		$this->crucial_get_param = $crucial_get_param;
		$this->get_params = Ajax::pureGet();
		$this->current_key = empty($this->get_params[$this->crucial_get_param]) ? false : $this->get_params[$this->crucial_get_param];
        if(is_array($this->current_key))
        	$this->current_key = isset($this->current_key["act"]) ? $this->current_key["act"] : false;
	}
	
	public function addItem($key, $title, $is_default = false, $url = false){
		$this->items[$key] = $title;
		if($is_default) $this->default_key = $key;
		if($url !== false) $this->setUrl($key, $url);
	}
	
	public function removeItem($key){
		unset($this->items[$key]);
	}
	
	public function addItems($arr){
		foreach($arr as $one) $this->addItem($one, $one);
	}
	
	public function removeItems(){
		$this->items = array();
	}
	
	public function addSeparator(){
		$this->addItem("__separator".count($this->items), "__separator");
	}
	
	public function getItemsCount(){
		return count($this->items);
	}
	
	public function setTitle($title){
		$this->title = $title;
	}
	
	public function setParams($key /* ... params*/){
		$params = func_get_args();
		array_shift($params);
		if(!count($params)) return;
		$this->action_params[$key] = $params;
	}
	
	public function getTitle(){
		return $this->title;
	}
	
	public function setHotkey($key, $hotkey){
		$this->hotkeys[$key] = $hotkey;
	}
	
	public function setUrl($key, $url){
		$this->urls[$key] = $url;
	}
	
	public function setDefault($key){
		$this->default_key = $key;
	}
	
	public function getCurrentItem(){
		if($this->current_key) return $this->current_key;
		if($this->default_key) return $this->default_key;
		if(count($this->items)) return array_shift(array_keys($this->items));
	}
	
	public function setCurrent($key){
		//if(!isset($this->items[$key])) throw new Exception("key not exists '$key'");
		$this->current_key = $key;
	}
	
	public function removeGetParam($key){
		if(!isset($this->get_params[$key])) return;
		unset($this->get_params[$key]);
	}
	
	public function keepGetParams($keys/*...*/){
		if(func_num_args() > 1 || !is_array($keys))
			$keys = func_get_args();
		if(!is_array($this->get_params)) return;
		$keys = array_flip($keys);
		$this->get_params = array_intersect_key($this->get_params, $keys);
	}
	
	public function htmlView(){
		$ret = "<div class='radio {$this->name}'>";
		if($this->getTitle()) $ret .= $this->getTitle();
		foreach($this->items as $key => $val){
			if($val == "__separator"){
				$ret .= "<span class='separator'>&nbsp;</span>";
				continue;
			}
            $uri = new UriConstructor($this->get_params);
            $params = isset($this->action_params[$key]) ? $this->action_params[$key] : false;
            if($params)
            	$uri->set($this->crucial_get_param, $key, $params);
            else
            	$uri->set($this->crucial_get_param, $key);
            $hotkey_str = "";
            if(!empty($this->hotkeys[$key])) $hotkey_str = " hotkey='{$this->hotkeys[$key]}'";
            $url = $uri->url();
            if(!empty($this->urls[$key])) $url = $this->urls[$key];
			if($this->getCurrentItem() == $key || in_array($key, $this->other_selected))
				$ret .= "<a href='{$url}' class='radio_item $key selected' $hotkey_str>$val</a>";
			else
				$ret .= "<a href='{$url}' class='radio_item $key' $hotkey_str>$val</a>";
		}
		$ret .= "</div>";
		return $ret;
	}
    
    public function getOutput(){
        return $this->htmlView();
    }
	
    public function addSelected($key){
        $this->other_selected[] = $key;
    }
}

?>
